import { asyncRoutes, constantRoutes } from '@/router'

const state = {
  routes: constantRoutes// 所有人默认拥有静态路由
}

const mutations = {
  setRoutes(state, newRoutes) {
    //   应该是在所有的静态路由基础上添加新的动态路由
    state.routes = [...constantRoutes, ...newRoutes]
  }
}

const actions = {
  // 筛选动态路由
  filterRoutes(store, menus) {
    console.log(17, menus)
    const routes = []
    menus.forEach(key => {
      routes.push(...asyncRoutes.filter(item => item.name === key)) // 得到一个数组
    })
    store.commit('setRoutes', routes)// 将动态路由提交给mutations
    console.log(23, routes)
    return routes
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
